﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OracleClient;

namespace Projekt_GUI.Forms
{
    public partial class BuchungToDo : Form
    {
        string id;
        OracleHelper oracle { get { return OracleHelper.Instance; } }
        public BuchungToDo()
        {
            InitializeComponent();
            try
            {
                oracle.Open();
                OracleDataReader r = oracle.Select("SQ_Buchungen.NEXTVAL", "dual");

                while (r.Read())
                {
                    id = r.GetValue(0).ToString();
                }
                oracle.Close();

            }
            catch (OracleException ex)
            {
                MessageBox.Show(ex.Message, "Oracle Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void BuchungToDo_Load(object sender, EventArgs e)
        {

        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            int ObjectCounter = 0;
            string Name = "", Straße = "", Ort = "", Land = "";
            #region Barcode

            string CODE = Spec.Barcode.getBCode(id);
            int position = 0;

            foreach (char c2 in CODE)
            {
                
                if (c2 == 'b')
                {
                    e.Graphics.DrawLine(new Pen(Brushes.Black, 2), new Point(650 + position, 40), new Point(650 + position, 70));
                    position += 2;
                }
                else
                {
                    position += 2;
                }
            }
 
            #endregion

            using (OracleConnection con = new OracleConnection("Data Source=localhost; User Id=PROJEKT; Password=PROJEKT"))
            {
                con.Open();
                OracleCommand com = con.CreateCommand();
                com.CommandText = "SELECT Name, Vorname, Strasse, PLZ, Ort, Land FROM Kunden WHERE Nummer = " + Form1.KundenID;

                OracleDataReader r = com.ExecuteReader();

                while (r.Read())
                {
                    Name = r.GetValue(0).ToString() +", "+r.GetValue(1).ToString();
                    Straße = r.GetValue(2).ToString();
                    Ort = r.GetValue(3).ToString() + " " + r.GetValue(4).ToString();
                    Land = r.GetValue(5).ToString();
                }
            }

            e.Graphics.DrawString(Name, new System.Drawing.Font("Arial", 10), Brushes.Black, new PointF(90,220));
            e.Graphics.DrawString(Straße, new System.Drawing.Font("Arial", 10), Brushes.Black, new PointF(90, 238));
            e.Graphics.DrawString(Ort, new System.Drawing.Font("Arial", 10), Brushes.Black, new PointF(90, 256));
            e.Graphics.DrawString(Land, new System.Drawing.Font("Arial", 10), Brushes.Black, new PointF(90, 274));

            e.Graphics.DrawLine(new Pen(Brushes.Black, 4), new Point(70, 410), new Point(750,410));
            e.Graphics.DrawLine(new Pen(Brushes.Black, 4), new Point(70, 437), new Point(750, 437));
            e.Graphics.DrawString("Invoice No.  " +id, new System.Drawing.Font("Arial", 12), Brushes.Black, new PointF(70, 413));
            e.Graphics.DrawString("Customer  " + Form1.KundenID, new System.Drawing.Font("Arial", 12), Brushes.Black, new PointF(300, 413));
            e.Graphics.DrawString("Date  " + DateTime.Now.ToShortDateString(), new System.Drawing.Font("Arial", 12), Brushes.Black, new PointF(610, 413));

            e.Graphics.DrawLine(new Pen(Brushes.Black, 1), new Point(70, 445), new Point(750, 445));
            e.Graphics.DrawLine(new Pen(Brushes.Black, 1), new Point(70, 467), new Point(750, 467));
            e.Graphics.DrawString("Article ID", new System.Drawing.Font("Arial", 12), Brushes.Black, new PointF(70, 447));
            e.Graphics.DrawString("Typ", new System.Drawing.Font("Arial", 12), Brushes.Black, new PointF(160, 447));
            e.Graphics.DrawString("Item", new System.Drawing.Font("Arial", 12), Brushes.Black, new PointF(240, 447));
            e.Graphics.DrawString("VAT.", new System.Drawing.Font("Arial", 12), Brushes.Black, new PointF(560, 447));
            e.Graphics.DrawString("Price", new System.Drawing.Font("Arial", 12), Brushes.Black, new PointF(670, 447));

            float preis_Summe = 0, Vat_Summe = 0;

            foreach (System.Collections.Generic.KeyValuePair<string, string> s in Form1.Cart)
            {
                string desc = "";
                float preis = 0;
                float Vat = 0;

                using (OracleConnection con = new OracleConnection("Data Source=localhost; User Id=PROJEKT; Password=PROJEKT"))
                {
                    con.Open();
                    OracleCommand com = con.CreateCommand();
                    com.CommandText = "SELECT Beschreibung, Preis FROM Reisen WHERE Nummer = " + s.Value;

                    OracleDataReader r = com.ExecuteReader();

                    while (r.Read())
                    {
                        desc = r.GetValue(0).ToString();
                        preis = float.Parse(r.GetValue(1).ToString());
                    }
                }

                if (preis != 0)
                    Vat = preis / 109 * 9;
                e.Graphics.DrawString(s.Value, new System.Drawing.Font("Arial", 12), Brushes.Black, new PointF(70, 469 + (ObjectCounter * 20)));
                e.Graphics.DrawString("Reise", new System.Drawing.Font("Arial", 12), Brushes.Black, new PointF(160, 469 + (ObjectCounter * 20)));
                e.Graphics.DrawString(desc, new System.Drawing.Font("Arial", 12), Brushes.Black, new PointF(240, 469 + (ObjectCounter * 20)));
                e.Graphics.DrawString(Vat.ToString("F2") + "€", new System.Drawing.Font("Arial", 12), Brushes.Black, new PointF(560, 469 + (ObjectCounter * 20)));
                e.Graphics.DrawString(preis.ToString("F2") + "€", new System.Drawing.Font("Arial", 12), Brushes.Black, new PointF(670, 469 + (ObjectCounter * 20)));
                ObjectCounter++;
                preis_Summe += preis;
                Vat_Summe += Vat;
            }
            foreach (System.Collections.Generic.KeyValuePair<string, string> s in Form1.CartExt)
            {
                string desc = "";
                float preis = 0;
                float Vat = 0;

                using (OracleConnection con = new OracleConnection("Data Source=localhost; User Id=PROJEKT; Password=PROJEKT"))
                {
                    con.Open();
                    OracleCommand com = con.CreateCommand();
                    com.CommandText = "SELECT Name, Preis FROM Erweiterungen WHERE Nummer = " + s.Value;

                    OracleDataReader r = com.ExecuteReader();

                    while (r.Read())
                    {
                        desc = r.GetValue(0).ToString();
                        preis = float.Parse(r.GetValue(1).ToString());
                    }
                }

                if (preis != 0)
                    Vat = preis / 109 * 9;
                e.Graphics.DrawString(s.Value, new System.Drawing.Font("Arial", 12), Brushes.Black, new PointF(70, 469 + (ObjectCounter * 20)));
                e.Graphics.DrawString("Erw.", new System.Drawing.Font("Arial", 12), Brushes.Black, new PointF(160, 469 + (ObjectCounter * 20)));
                e.Graphics.DrawString(desc, new System.Drawing.Font("Arial", 12), Brushes.Black, new PointF(240, 469 + (ObjectCounter * 20)));
                e.Graphics.DrawString(Vat.ToString("F2") + "€", new System.Drawing.Font("Arial", 12), Brushes.Black, new PointF(560, 469 + (ObjectCounter * 20)));
                e.Graphics.DrawString(preis.ToString("F2") + "€", new System.Drawing.Font("Arial", 12), Brushes.Black, new PointF(670, 469 + (ObjectCounter * 20)));
                ObjectCounter++;
                preis_Summe += preis;
                Vat_Summe += Vat;
            }

            e.Graphics.DrawLine(new Pen(Brushes.Black, 1), new Point(70, 469 + (ObjectCounter * 20)), new Point(750, 469 + (ObjectCounter * 20)));
            ObjectCounter++;
            e.Graphics.DrawLine(new Pen(Brushes.Black, 1), new Point(70, 469 + (ObjectCounter * 20)), new Point(750, 469 + (ObjectCounter * 20)));
            ObjectCounter++;
            ObjectCounter++;

            e.Graphics.DrawString("Netprice", new System.Drawing.Font("Arial", 12), Brushes.Black, new PointF(500, 469 + (ObjectCounter * 20)));
            e.Graphics.DrawString((preis_Summe - Vat_Summe).ToString("F2") + "€", new System.Drawing.Font("Arial", 12), Brushes.Black, new PointF(670, 469 + (ObjectCounter * 20)));
            ObjectCounter++;
            ObjectCounter++;

            e.Graphics.DrawString("VAT", new System.Drawing.Font("Arial", 12), Brushes.Black, new PointF(500, 469 + (ObjectCounter * 20)));
            e.Graphics.DrawString((Vat_Summe).ToString("F2") + "€", new System.Drawing.Font("Arial", 12), Brushes.Black, new PointF(670, 469 + (ObjectCounter * 20)));
            ObjectCounter++;
            ObjectCounter++;

            e.Graphics.DrawString("Final", new System.Drawing.Font("Arial", 12), Brushes.Black, new PointF(500, 469 + (ObjectCounter * 20)));
            e.Graphics.DrawString((preis_Summe).ToString("F2") + "€", new System.Drawing.Font("Arial", 12), Brushes.Black, new PointF(670, 469 + (ObjectCounter * 20)));

            e.Graphics.Flush();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                DialogResult res = printDialog1.ShowDialog();

                if (res == System.Windows.Forms.DialogResult.OK)
                {
                    printDocument1.PrinterSettings = printDialog1.PrinterSettings;
                    printDocument1.Print();
                }
            }


            Close();
        }
    }
}
